class socket {
	constructor() {
		this.connection = {};
	}
	// 创建一个TCP实例
	establish(address, port) {
		console.log('开始创建')
		this.connection = wx.createTCPSocket();
		// this.connection.connect({ address: "000.000.0.0", port: 6800 });
		this.connection.connect({
			address,
			port,
			timeout: 3
		});
		console.log('开始连接', JSON.stringify({
			address,
			port,
			timeout: 3
		}))
	}
	// 发送消息
	connect(message) {
		console.log('发送消息', message)
		this.connection.onConnect(() => {
			this.connection.write(message);
		});
	}
	// 监听端口函数
	onMessage(success, failure) {
		console.log('onMessage')
		this.connection.onMessage((res) => {
			console.log('走了成功')
			success(res)
		});
		this.connection.onError((err) => {
			console.log('走了失败')
			failure(err);
		});
	}

	// 关闭搜索事件
	TCPclose() {
		setTimeout(() => {
			// 3S后，关闭socket
			this.connection.close(() => {
				console.log("TCP关闭");
			});
		}, 3000);
	}
}
const TCPSocket = new socket();
export default TCPSocket;